package ling.archives.domain.dto;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import ling.archives.common.ExcelHandle;
import ling.archives.common.ValidUtil;
import ling.archives.domain.ArchiveContent;
import lombok.Data;
import lombok.var;
import org.apache.poi.ss.usermodel.Row;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;

/** 档案记录类
 *
 */
@Data
public class  ArchiveContentXls implements ExcelHandle.rowSetHeader{
    public static List<String> check(ArchiveContentXls ...objs) {
        var list = new ArrayList<String>();
        for (var item : objs) {
            var msg = ValidUtil.validToStr(item);
            if (StringUtils.isNotBlank(msg))
                list.add("第"+(item.dataRowNum+1)+"行:"+msg);
        }
        return list;
    }

    public ArchiveContent toEntity() {
        var content = new ArchiveContent();
        content.setStatus("0");
        content.setFullArchiveName(全宗名称);
        content.setFullNum(文件级档号);
        content.setTitle(文件题名);
        content.setYear(Integer.parseInt(年度));
        content.setFullArchiveNum(全宗号);
        content.setNum(案卷号);
        content.setIndex(Integer.parseInt(formatToThreeDigits(卷内顺序号)));
        content.setManager(责任者);
        content.setFileDate(StringUtils.isBlank(日期) || "未标明日期".equals(日期) || "未标明".equals(日期) ? null : DateUtils.dateTime("yyyyMMdd", 日期));
        content.setPageIndex(Integer.parseInt(页次));
        content.setPageCount(Integer.parseInt(页数));
        //
        var infos = ArchiveXls.strGroupsByRegex(文件题名, titleReg);
        content.setVillageName(infos[1]);
        content.setHouseholdHead(infos[2]);
        content.setTownName(infos[0]);
        var numInfos = ArchiveXls.strGroupsByRegex(文件级档号, fullNumReg);
        if (numInfos.length == 3) {
            var indexTmp = numInfos[2];
            var arNum = 文件级档号.substring(0, 文件级档号.length() - indexTmp.length() - 1);
            content.setArchiveNum(arNum);
        }
        //

//        filesPathSet(content);
        return content;
    }

//    private static void filesPathSet(ArchiveContent content) {
//        var pathJson=ArchiveContent.gainFilesPathJson(content,null);
//        content.setPageFiles(pathJson);
//    }

    //public final static String  hz="(?:[\\u3400-\\u4DBF\\u4E00-\\u9FFF\\uF900-\\uFAFF]|[\\uD840-\\uD868\\uD86A-\\uD872\\uD87C-\\uD879][\\uDC00-\\uDFFF]|\\uD869[\\uDC00-\\uDEDF\\uDF00-\\uDFFF]|\\uD86D[\\uDC00-\\uDF3F]|\\uD86E[\\uDC00-\\uDC1F]|\\uD87E[\\uDC00-\\uDE1F])";
    //"^([\\u4e00-\\u9fa5]+[乡|镇])（镇）-([\\u4e00-\\u9fa5]+村)-([\\u4e00-\\u9fa5]{2,})-农宅存根$"
    /** 乡镇、村、人
     *
     */
    public final static String  titleReg="^("+ArchiveXls.hzReg +"+[乡|镇])(?:[\\\\(（]镇[\\\\)）])?-("+ArchiveXls.hzReg +"+)-("+ArchiveXls.hzReg +"+(?:（"+ArchiveXls.hzReg +"+）)?(?:、"+ArchiveXls.hzReg +"+(?:（"+ArchiveXls.hzReg +"+）)?)*)-(?:农宅存根|用地许可证存根|农宅用地许可证|农宅更换补办|农房建设许可证|宅基地验收表|占地完善用地手续通知书)$";

    /**
     * 将数字转换为三位数格式（补零）
     * @param number 原始数字字符串
     * @return 补零后的三位数字符串
     */
    public static String formatToThreeDigits(String number) {
        if (number == null || number.trim().isEmpty()) {
            return "000";
        }
        try {
            int num = Integer.parseInt(number.trim());
            return String.format("%03d", num);
        } catch (NumberFormatException e) {
            return "000";
        }
    }

    /** 年份、档号、文件序号
     *
     */
    public final static String  fullNumReg="^0099-(?:(?:用地许可证存根·|农宅用地许可证·|农宅更换补办·|农房建设许可证·|宅基地验收表·|占地完善用地手续通知书·)?((?:19|20)\\d{2}))-永久-(\\d{4})-(\\d{3})$";
    @Excel(name = "档案馆代码")
    private String 档案馆代码;

    @Excel(name = "著录层级")
    private String 著录层级;


    @Excel(name = "全宗名称")
    private String 全宗名称;

    @Excel(name = "档案门类代码")
    private String 档案门类代码;

    @Pattern(message = "格式错误",regexp = "\\d{4}")
    @Excel(name = "全宗号")
    private String 全宗号;

    @Excel(name = "保管期限")
    private String 保管期限;

    @Digits(integer = 4,fraction = 0)
    @Excel(name = "年度")
    private String 年度;

    @Excel(name = "目录号")
    private String 目录号;

    @NotBlank
    @Pattern(regexp =fullNumReg,message = "格式错误")
    @Excel(name = "文件级档号")
    private String 文件级档号;

    @Pattern(message = "格式错误",regexp = "\\d{4}")
    @Excel(name = "案卷号")
    private String 案卷号;

    @Excel(name = "卷内顺序号")
    @Pattern(regexp = "\\d{3}", message = "格式错误")
    private String 卷内顺序号;

    public void set卷内顺序号(String 卷内顺序号) {
        this.卷内顺序号 = formatToThreeDigits(卷内顺序号);
    }

    @Excel(name = "文件编号")
    private String 文件编号;

    @NotBlank
    @Pattern(regexp = titleReg,message = "格式错误")
    @Excel(name = "文件题名")
    private String 文件题名;


    @Pattern(regexp = "^(?:(?:19|20)\\d{2}[01]\\d[0123]\\d|未标明日期|未标明)?$", message = "日期格式应为yyyyMMdd或未标明日期或未标明")
    @Excel(name = "日期")
    private String 日期;

    @Excel(name = "责任者")
    private String 责任者;


    @Digits(integer = 4,fraction = 0)
    @Excel(name = "页次")
    private String 页次;

    @Digits(integer = 4,fraction = 0)
    @Excel(name = "页数")
    private String 页数;

    @Excel(name = "机构或问题")
    private String 机构或问题;

    @Excel(name = "密级")
    private String 密级;

    @Excel(name = "主题词或关键词")
    private String 主题词或关键词;

    @Excel(name = "备注")
    private String 备注;

    @Excel(name = "存放位置")
    private String 存放位置;

    @Excel(name = "开放标识")
    private String 开放标识;

    @Excel(name = "分类号")
    private String 分类号;

    @Excel(name = "稿本")
    private String 稿本;

    @Excel(name = "文种")
    private String 文种;

    @Excel(name = "附件")
    private String 附件;

    @Excel(name = "载体形态")
    private String 载体形态;

    @Excel(name = "计算机文件大小")
    private String 计算机文件大小;

    @Excel(name = "计算机文件格式")
    private String 计算机文件格式;

    @Excel(name = "生成方式")
    private String 生成方式;

    @Excel(name = "销毁情况")
    private String 销毁情况;

    @Excel(name = "公开属性")
    private String 公开属性;

    @Excel(name = "文件所涉人名")
    private String 文件所涉人名;

    @Excel(name = "缩微号")
    private String 缩微号;

    @Excel(name = "档案门类")
    private String 档案门类;

    @Excel(name = "盒号")
    private String 盒号;

    @Excel(name = "档案开放")
    private String 档案开放;

    @Excel(name = "开放控制")
    private String 开放控制;

    @Excel(name = "开放状态")
    private String 开放状态;

    @Excel(name = "实体份数")
    private String 实体份数;

    @Excel(name = "流程分组状态")
    private String 流程分组状态;

    @Excel(name = "档案划控状态")
    private String 档案划控状态;

    @Excel(name = "档案销毁状态")
    private String 档案销毁状态;

    @Excel(name = "划控鉴定审批状态")
    private String 划控鉴定审批状态;

    @Excel(name = "销毁鉴定审批状态")
    private String 销毁鉴定审批状态;

    @Excel(name = "密级鉴定审批状态")
    private String 密级鉴定审批状态;

    @Excel(name = "移交审批状态")
    private String 移交审批状态;

    @Excel(name = "移交失败原因")
    private String 移交失败原因;

    @Excel(name = "归档审批状态")
    private String 归档审批状态;

    @Excel(name = "归档失败原因")
    private String 归档失败原因;


    Integer dataRowNum;
    @Override
    public void after(Row row) {
        dataRowNum=row.getRowNum();
    }
}
